package com.tengju.bff.interfaces.manage.crm.dashborad;

import com.tengju.bff.interfaces.manage.crm.dashborad.facade.BrandAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.PerformanceFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.SecMarketAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.SecPullingAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.PerformanceVO;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecMarketAnalysisVO;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecPullingAnalysisVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

@Validated
@RestController
@Api(tags = "总监CRM-仪表盘管理")
@RequestMapping(value = "manage/crm/dashboard")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DashboardController {

    private final SecMarketAnalysisFacade secMarketAnalysisFacade;
    private final BrandAnalysisFacade brandAnalysisFacade;
    private final PerformanceFacade performanceFacade;
    private final SecPullingAnalysisFacade secPullingAnalysisFacade;

    @ApiOperation(value = "绩效目标", notes = "绩效目标")
    @RequestMapping(value = "/performance", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PerformanceVO> performance(@NotNull(message = "俱乐部id不能为空") Long clubId,
                                                  @NotNull(message = "季度不能为空") String quarter) {
        try {
            return ApiResponse.newSuccess(performanceFacade.performance(clubId, quarter));
        } catch (Exception e) {
            log.error("error", e);
            return ApiResponse.newFailure(ResultCodeEnum.SYS_EXCEPTION.getCode(),
                    ResultCodeEnum.SYS_EXCEPTION.getMsg());
        }
    }

    @ApiOperation(value = "SEC市场分析", notes = "SEC市场分析")
    @RequestMapping(value = "/secMarketAnalysis", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SecMarketAnalysisVO> secMarketAnalysis(@NotNull(message = "俱乐部id不能为空") Long clubId) {
        return ApiResponse.newSuccess(secMarketAnalysisFacade.secMarketAnalysis(clubId));
    }

    @ApiOperation(value = "品牌销售排行", notes = "品牌销售排行")
    @RequestMapping(value = "/brand", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> brand(@NotNull(message = "月份不能为空") Integer month ,
                                      @NotNull(message = "俱乐部id不能为空") Long clubId) {
        return ApiResponse.newSuccess(brandAnalysisFacade.brand(month, clubId));
    }

    @ApiOperation(value = "SEC招商分析", notes = "SEC招商分析")
    @RequestMapping(value = "/secPullingAnalysis", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SecPullingAnalysisVO> secPullingAnalysis(@NotNull(message = "俱乐部id不能为空") Long clubId) {
        return ApiResponse.newSuccess(secPullingAnalysisFacade.secPullingAnalysis(clubId));
    }

    @ApiOperation(value = "SEC招商分析table", notes = "SEC招商分析table")
    @RequestMapping(value = "/secPullingTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> secPullingTable(@NotNull(message = "俱乐部id不能为空") Long clubId,
                                                @NotNull(message = "星级等级不能为空") Integer starLevel) {
        return ApiResponse.newSuccess(secPullingAnalysisFacade.secPullingTable(clubId, StarLevelEnum.parseByStarLevel(starLevel)));
    }
}
